package com.handpay.zztong.hp;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.handpay.crypto.Caesar;
import com.handpay.framework.ClientEngine;
import com.handpay.framework.utils.DES2PWD;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.paymentbill.model.Bill;
import com.handpay.zztong.hp.config.ZZTConfig;
import com.handpay.zztong.hp.login.activity.FindPwdActivity;
import com.handpay.zztong.hp.paymentbill.activity.QueryBillsActivity;
import com.handpay.zztong.hp.login.widget.LoginTextItemView;
import com.handpay.zztong.hp.utils.AccountUtils;

import java.util.Hashtable;

/**
 * 撤销交易
 *
 * @author fplei
 */
public class RescissionActivity extends ZZTongActivity {
    private InputMethodManager im = null;
    private Button ok_button = null;
    private Bill mBill;
    private TextView txt_user_account;
    private Runnable mNext;
    private LoginTextItemView et_pwd;
    private Caesar caesar;

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(this.getString(R.string.trans_details), true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_rescission);
        super.onCreate(savedInstanceState);
        caesar = new Caesar();
        mBill = (Bill) getIntent().getSerializableExtra(QueryBillsActivity.billvalue);
        initview();
    }

    private void initview() {
        et_pwd = (LoginTextItemView) findViewById(R.id.textview_pwd);
//	    im = ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)); 
//		im.showSoftInput(et_pwd, 0);
        txt_user_account = (TextView) findViewById(R.id.txt_user_account);
        txt_user_account.setText(AccountUtils.getRMSAccountName());
        findViewById(R.id.tv_forgetowd).setOnClickListener(this);
        ok_button = (Button) findViewById(R.id.ok_button);
        ok_button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
//        boolean isCheckedVersion = (Boolean) ClientEngine.getInstance().getGlobal(ZZTongActivity.VerifyVersionOK, false);
        if (R.id.ok_button == v.getId()) {
            String intput_pwd = et_pwd.getText().toString();
            if (intput_pwd != null && intput_pwd.length() > 0) {
                String localpwd = AccountUtils.getRMSAccountPwd();
                try {
                    intput_pwd = DES2PWD.encryptDES(Caesar.caesarEncryption(intput_pwd.trim()), ZZTConfig.SECRETKEY);
//						HPLog.i(HPLog.LFP_TAG, "pwdDes{localpwd:"+localpwd+",intput_pwd:"+intput_pwd+"}");
                    if (localpwd.equals(intput_pwd)) {
//		    				im.hideSoftInputFromWindow(et_pwd.getWindowToken(), 0);
                        showAlertDialog(this, getString(R.string.tip), getString(R.string.revoke_inputpwd_confirm_tip), false, getString(R.string.revoke_confirm_ok), new OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(RescissionActivity.this, RescissionResultActivity.class);
                                intent.putExtra(QueryBillsActivity.billvalue, mBill);
                                startActivity(intent);
                                finish();
                            }
                        }, getString(R.string.cancel), new OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                    } else {
                        Toast.makeText(this, "密码不正确，请重新输入！", Toast.LENGTH_LONG).show();
                        et_pwd.setText("");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                showAlertDialog(this, getString(R.string.tip), getString(R.string.revoke_inputpwd_null_tip), false, new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
            }
        } else if (R.id.tv_forgetowd == v.getId()) {
                goToFindPassword();
        }
    }

    @Override
    protected boolean isShownTopBar() {
        return true;
    }

    private void goToFindPassword() {
        Intent intent = new Intent(this, FindPwdActivity.class);
        this.startActivity(intent);
    }

    @Override
    public boolean netResponse(String action, Hashtable<String, Object> resp,
                               boolean showAlert, OnClickListener on) {
        if (Do_ZztNewVerifyVersion.equals(action)) {
            boolean result = super.netResponse(action, resp, showAlert, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    RescissionActivity.this.finish();
                }
            });
            boolean versionOk = (Boolean) ClientEngine.getInstance().getGlobal(ZZTongActivity.VerifyVersionOK, false);
            if (true == versionOk && null != mNext) {
                mNext.run();
                mNext = null;
            }
            return result;
        }
        return super.netResponse(action, resp, showAlert, on);
    }

}
